//
//  FQDColumnHeaderImageCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/23/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDColumnHeaderImageCell.h"
#import "FQDSubscribeManager.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

static NSString *const kSubscribeImageName = @"columnInfoSubscribe";
static NSString *const kUnSubscribeImageName = @"columnInfoUnsubscribe";

@interface FQDColumnHeaderImageCell ()

@property (nonatomic, strong) UIImageView *titleImageView;
@property (nonatomic, strong) UIImageView *maskImageView;
@property (nonatomic, strong) UILabel *columnNameLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIButton *subscribeButton;

@end

@implementation FQDColumnHeaderImageCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareCell];
        [self prepareCellConstraints];
    }
    return self;
}

- (void)prepareCell
{
    _titleImageView = [[UIImageView alloc] init];
    _titleImageView.contentMode = UIViewContentModeScaleAspectFill;
    _titleImageView.clipsToBounds = YES;
    [self.contentView addSubview:_titleImageView];
    
    _maskImageView = [[UIImageView alloc] init];
    _maskImageView.contentMode = UIViewContentModeScaleAspectFill;
    _maskImageView.clipsToBounds = YES;
    [self.contentView addSubview:_maskImageView];
    
    _columnNameLabel = [[UILabel alloc] init];
    _columnNameLabel.font = FQDBoldFontWithSize(26.0f);
    _columnNameLabel.textColor = [UIColor whiteColor];
    _columnNameLabel.textAlignment = NSTextAlignmentCenter;
    _columnNameLabel.numberOfLines = 0;
    _columnNameLabel.preferredMaxLayoutWidth = 240.0f;
    [_titleImageView addSubview:_columnNameLabel];
    
    _descLabel = [[UILabel alloc] init];
    _descLabel.font = FQDRegularFontWithSize(14.0f);
    _descLabel.textColor = [UIColor whiteColor];
    _descLabel.textAlignment = NSTextAlignmentCenter;
    _descLabel.numberOfLines = 0;
    _descLabel.preferredMaxLayoutWidth = 250.0f;
    [_titleImageView addSubview:_descLabel];
    
    UIImage *subscribeImage = [UIImage imageNamed:kSubscribeImageName];
    _subscribeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_subscribeButton setImage:subscribeImage forState:UIControlStateNormal];
    [self.contentView addSubview:_subscribeButton];
    
    [self.subscribeButton addTarget:self action:@selector(subscribeColumn:)
                   forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)prepareCellConstraints
{
    WeakSelf;
    [_maskImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.titleImageView);
    }];
    
    [_columnNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView).offset(40);
        make.width.equalTo(weakSelf.titleImageView).offset(-80);
        make.bottom.equalTo(weakSelf.descLabel.mas_top).offset(-6);
    }];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView).offset(35);
        make.width.equalTo(weakSelf.titleImageView).offset(-70);
        make.bottom.equalTo(weakSelf.subscribeButton.mas_top).offset(-6);
    }];
    
    [_titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.contentView);
    }];
    
    [_subscribeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.contentView);
        make.bottom.equalTo(weakSelf.contentView).offset(-10);
        make.size.equalTo(@50);
    }];
}

- (void)subscribeColumn:(UIButton *)subscribeButton
{
    if ([self.delegate respondsToSelector:@selector(collectionViewCell:actionType:actionControl:userInfo:)]) {
        [self.delegate collectionViewCell:self actionType:FQDColumnHeaderImageCellActionSubscribe
                            actionControl:subscribeButton userInfo:nil];
    }
}


- (void)setColumn:(FQDColumn *)column
{
    _column = column;
    
    NSURL *imageURL = [NSURL URLWithString:column.image];
    [self.titleImageView sd_setImageWithURL:imageURL];
    
    self.columnNameLabel.text = column.name;
    self.descLabel.text = column.desc;
    
    
    FQDSubscribeManager *subscribeManager = [FQDSubscribeManager sharedManager];
    BOOL hasSubscribed = [subscribeManager hasSubscribedColumn:column.ID];
    self.subscribed = hasSubscribed;
}

- (void)setSubscribed:(BOOL)subscribed
{
    _subscribed = subscribed;
    
    NSString *subscribeImageName = subscribed ? kUnSubscribeImageName : kSubscribeImageName;
    [self.subscribeButton setImage:[UIImage imageNamed:subscribeImageName] forState:UIControlStateNormal];
}

+ (CGFloat)cellHeight
{
    return 240.0f;
}

+ (NSString *)reuseId
{
    return @"FQDColumnHeaderImageCell";
}

@end
